package force;

import java.awt.event.KeyEvent;
import java.util.Scanner;
import object.mass.Mass;
import sim.Simulation;

/**
 * A representation of the force of gravity.
 * @author Niel Lebeck and Wynne Lok
 *
 */
public class Gravity extends GlobalForceSource {
    private static final double DEFAULT_GRAVITY_DIR = 90;
    private static final double DEFAULT_GRAVITY_MAG = 1;
    
    private boolean myGravityOn;
    private double myGravityDir;
    private double myGravityMag;
    
    /**
     * 
     * @param line String
     */
    public Gravity(String line) {
        Scanner s = new Scanner(line);
        String type = s.next();
        myGravityDir = s.nextDouble();
        myGravityMag = s.nextDouble();
        myGravityOn = true;
    }
    
    /**
     * 
     */
    public Gravity() {
        myGravityDir = DEFAULT_GRAVITY_DIR;
        myGravityMag = DEFAULT_GRAVITY_MAG;
        myGravityOn = false;
    }
    
    /**
     * Calculate the force of gravity on the given Mass.
     * @param m Mass
     * @param sim Simulation
     * @return
     */
    @Override
    public Force getForce (Mass m, Simulation sim) {
        Force result;
        if (myGravityOn) {
            result = new Force(myGravityDir, myGravityMag);
            result.scale(1.0 / m.getMass());
        }
        else {
            result = new Force(0, 0);
        }
        return result;
    }

    @Override
    public void toggle(int keycode) {
        if (keycode == KeyEvent.VK_G) {
            myGravityOn = !myGravityOn;
        }
    }

}
